import 'package:decimal/decimal.dart';
import 'package:flutter_deer/database/beans/get_merchant_configer.dart';


extension AmountCalculation on String {
  /// 金额加法
  String plus(String other) {
    try {
      final thisDecimal = Decimal.parse(this);
      final otherDecimal = Decimal.parse(other);
      return (thisDecimal + otherDecimal).toString();
    } catch (e) {
      throw ArgumentError("Invalid number format: $this or $other");
    }
  }

  /// 金额减法
  String minus(String? other) {
    try {
      print('----->金额减法: $this - $other');
      Decimal thisDecimal = Decimal.tryParse(this?? '0.00') ?? Decimal.zero;
      Decimal otherDecimal = Decimal.tryParse(other ?? '0.00') ?? Decimal.zero;
      return (thisDecimal - otherDecimal).toString();
    } catch (e) {
      throw ArgumentError("Invalid number format: $this or $other");
    }
  }

  /// 金额乘法
  String multiply(String other) {
    try {
      final thisDecimal = Decimal.parse(this);
      final otherDecimal = Decimal.parse(other);
      return (thisDecimal * otherDecimal).toString();
    } catch (e) {
      throw ArgumentError("Invalid number format: $this or $other");
    }
  }

  /// 金额除法
  /// [scale] 保留小数位数
  /// [rounding] 舍入模式，默认为四舍五入
  String divide(String other, {int scale = 2, RoundingMode rounding = RoundingMode.round}) {
    try {
      final thisDecimal = Decimal.parse(this);
      final otherDecimal = Decimal.parse(other);
      if (otherDecimal == Decimal.zero) {
        throw ArgumentError("Division by zero is not allowed");
      }
      Decimal result;
      switch (rounding) {
        case RoundingMode.round:
          result = (thisDecimal / otherDecimal).toDecimal(scaleOnInfinitePrecision: scale).round(scale: scale);
          break;
        case RoundingMode.ceil:
          result = (thisDecimal / otherDecimal).toDecimal(scaleOnInfinitePrecision: scale).ceil(scale: scale);
          break;
        case RoundingMode.floor:
          result = (thisDecimal / otherDecimal).toDecimal(scaleOnInfinitePrecision: scale).floor(scale: scale);
          break;
      }
      return result.toStringAsFixed(scale);
    } catch (e) {
      throw ArgumentError("Invalid number format: $this or $other");
    }
  }
  /// 金额除法
  /// [scale] 保留小数位数
  /// [rounding] 舍入模式，默认为四舍五入
  String divideDefault(String other) {
    try {
      final thisDecimal = Decimal.parse(this);
      final otherDecimal = Decimal.parse(other);
      if (otherDecimal == Decimal.zero) {
        throw ArgumentError("Division by zero is not allowed");
      }
      int scale = 2;
      final config = GetMerchantPreferentialConfigEntity.get();
      Decimal? result;
      switch (config?.roundingMode) {
        case 1:// 四舍五入
          result = (thisDecimal / otherDecimal).toDecimal(scaleOnInfinitePrecision: scale).round(scale: scale);
          break;
        case 2:// 进位
          result = (thisDecimal / otherDecimal).toDecimal(scaleOnInfinitePrecision: scale).ceil(scale: scale);
          break;
        case 3:// 抹掉
          result = (thisDecimal / otherDecimal).toDecimal(scaleOnInfinitePrecision: scale).floor(scale: scale);
          break;
      }
      return result?.toStringAsFixed(scale) ?? '0.00';
    } catch (e) {
      throw ArgumentError("Invalid number format: $this or $other");
    }
  }

  /// 格式化金额显示（保留两位小数）
  String formatAmount() {
    try {
      final decimal = Decimal.parse(this);
      return decimal.toStringAsFixed(2);
    } catch (e) {
      throw ArgumentError("Invalid number format: $this");
    }
  }
}

// 定义舍入模式枚举
enum RoundingMode {
  round, // 四舍五入
  ceil,  // 进位
  floor  // 抹掉
}